<?php
class Muser extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	
	function addUser($data) {
		$this->db->insert ( 'user', $data );
		return $this->db->insert_id ();
	}
	
	function active($id, $code) {
		$this->db->select ( 'id,activation' );
		$this->db->where ( 'id', $id );
		$this->db->from ( 'user' );
		$query = $this->db->get ();
		if ($query->num_rows () > 0) {
			$row = $query->row ();
			if (strcmp ( $row->activation, $code ) == 0) {
				$data = array ('activation' => '#' );
				$this->db->where ( 'ID', $id );
				$this->db->update ( 'user', $data );
				return true;
			} else
				return false;
		} else
			return false;
	}
	
	function checkReset($id, $code) {
		$this->db->select ( 'id' );
		$this->db->where ( 'id', $id );
		$this->db->where ( 'reset', $code );
		$this->db->from ( 'user' );
		$query = $this->db->get ();
		if ($query->num_rows () > 0)
			return true;
		else
			return FALSE;
	}
	
	function selectAllUserforMail() {
		$this->db->select ( 'id,email,name' );
		$this->db->where ( 'activation', '#' );
		$query = $this->db->get ( 'user' );
		return $query->result_array ();
	}
	
	function checkEmail($email) {
		$this->db->select ( 'id' );
		$this->db->where ( 'email', $email );
		$query = $this->db->get ( 'user' );
		if ($query->num_rows () > 0) {
			return FALSE;
		} else
			return TRUE;
	}
	
	function getUserbyId($id) {
		$this->db->select ( 'email,name,mobile' );
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'user' );
		return $query->row_array ();
	}
	
	function getUserIdbyEmailPwd($email, $pwd, $ip) {
		$this->db->select ( 'id,name,activation' );
		$this->db->where ( 'email', $email );
		$this->db->where ( 'password', $pwd );
		$query = $this->db->get ( 'user' );
		if ($query->num_rows () > 0) {
			$data = array ('loginTime' => date ( "Y/m/d H:i:s", time () ), 'loginIp' => $ip );
			$this->db->where ( 'email', $email );
			$this->db->update ( 'user', $data );
			return $query->row_array ();
		} else
			return NULL;
	}
	
	function updateReset($str, $email) {
		$data = array ('reset' => $str );
		$this->db->where ( 'email', $email );
		$this->db->update ( 'user', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getIdNamebyEmail($email) {
		$this->db->select ( 'id,name' );
		$this->db->where ( 'email', $email );
		$query = $this->db->get ( 'user' );
		return $query->row_array ();
	}
	
	function resetPwd($id, $reset, $pwd) {
		$data = array ('reset' => '#', 'password' => $pwd );
		$this->db->where ( 'id', $id );
		$this->db->where ( 'reset', $reset );
		$this->db->update ( 'user', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getActivation($id) {
		$this->db->select ( 'activation,name,email' );
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'user' );
		return $query->row_array ();
	}
	
	function checkPwdbyId($pwd, $id) {
		$this->db->select ( 'id' );
		$this->db->where ( 'id', $id );
		$this->db->where ( 'password', $pwd );
		$query = $this->db->get ( 'user' );
		if ($query->num_rows () > 0) {
			return TRUE;
		} else
			return FALSE;
	}
	
	function modification($data, $id) {
		$this->db->where ( 'id', $id );
		$this->db->update ( 'user', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getUserNum() {
		return $this->db->count_all_results ( 'user' );
	}
	
	function getUserMaxNum(){
		$this->db->select_max ( 'id', 'num' );
		$query = $this->db->get ( 'user' );
		$num = $query->row_array ();
		return $num ['num'];
	}
	
	function getUserList($page, $num) {
		$this->db->order_by ( "creatTime", "desc" );
		$this->db->limit ( $num, $page );
		$query = $this->db->get ( 'user' );
		return $query->result_array ();
	}
	
	function getUserListforDownload($begin, $end) {
		$this->db->where ( "id >= $begin" );
		$this->db->where ( "id <= $end" );
		$this->db->order_by ( "id", "asc" );
		$query = $this->db->get ( 'user' );
		return $query->result_array ();
	}
	
	function deleteUser($id) {
		$this->db->where ( 'id', $id );
		$this->db->delete ( 'user' );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
}